HAL RNG initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 RNG configuration panel.

  • Configure the SW layer HAL.

  • Configure the RNG main features.

  • Configure the additional RNG features if needed.

  • Configure the NVIC if the application requires the RNG to be used in IT mode.

  • Generate the code.

Note

The configuration below might varies from a series to another.

HAL RNG configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL RNG main features configuration

Configure the RNG main features.

RNG main feature configuration

HAL RNG additional features configuration

Configure the additional RNG features if needed.

RNG additional feature configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

RNG NVIC configuration

Generated RNG initialization and de-initialization sequences

The RNG initialization sequence is generated in the mx_rng_default_hal_init function in mx_rng.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated: RNG initialization and de-initialization sequences

Topic

Code Snippet HAL1

Code Snippet HAL2

Handle declaration

RNG_HandleTypeDef hrng;
static hal_rng_handle_t hRNG;

Initialization and configuration sequence

static void MX_RNG_Init(void)
{

  hrng.Instance = RNG;
  hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
  if (HAL_RNG_Init(&hrng) != HAL_OK)
  {
    Error_Handler();
  }

}












void HAL_RNG_MspInit(RNG_HandleTypeDef* hrng)
{
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(hrng->Instance==RNG)
  {

    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RNG;
    PeriphClkInit.RngClockSelection = RCC_RNGCLKSOURCE_HSI48;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_RNG_CLK_ENABLE();
  }

}
hal_rng_handle_t *mx_rng_hal_init(void)
{
    HAL_RCC_RNG_EnableClock();

    if (HAL_RNG_Init(&hRNG, HAL_RNG) != HAL_OK)
    {
        return NULL;
    }

    /* Code HAL RNG Config....*/
    HAL_RNG_SetCandidateNISTConfig(&hRNG);
    if (HAL_RCC_RNG_SetKernelClkSource(HAL_RCC_RNG_CLK_SRC_HSI48) != HAL_OK)
    {
        return NULL;
    }

    HAL_RNG_EnableClockErrorDetection(&hRNG);

    /* Enable the interruption for RNG */

    return &hRNG;
}

Deinitialization sequence

void MX_RNG_DeInit(void)
{
  if (HAL_RNG_DeInit(&hrng) != HAL_OK)
  {
    Error_Handler();
  }
}





void HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng)
{
  if(hrng->Instance==RNG)
  {
    __HAL_RCC_RNG_CLK_DISABLE();

    __HAL_RCC_RNG_FORCE_RESET();

    __HAL_RCC_RNG_RELEASE_RESET();

  }

}
void mx_rng_hal_deinit(void)
{
    /* Disable the interruption for RNG */

    (void)HAL_RNG_DeInit(&hRNG);

    HAL_RCC_RNG_Reset();

    HAL_RCC_RNG_DisableClock();
}